//package com.liulimi.fastapi.config;
//
//import cn.hutool.json.JSON;
//import com.liulimi.fastapi.core.http.response.Response;
//import com.liulimi.fastapi.security.MyUsernamePasswordAuthenticationFilter;
//import com.liulimi.fastapi.utils.json.JsonUtil;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.context.annotation.Bean;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.security.authentication.AuthenticationManager;
//import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
//import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
//import org.springframework.security.config.annotation.web.builders.HttpSecurity;
//import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
//import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
//import org.springframework.security.config.http.SessionCreationPolicy;
//import org.springframework.security.core.AuthenticationException;
//import org.springframework.security.core.userdetails.User;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.security.core.userdetails.UsernameNotFoundException;
//import org.springframework.security.crypto.password.NoOpPasswordEncoder;
//import org.springframework.security.crypto.password.PasswordEncoder;
//import org.springframework.security.provisioning.InMemoryUserDetailsManager;
//import org.springframework.security.web.authentication.AuthenticationFailureHandler;
//import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
//import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
//import org.w3c.dom.UserDataHandler;
//
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//
///**
// * @author Wenbo
// * @date 2021/1/21 10:04
// * @Email 969100115@qq.com
// * @phone 17621847037
// */
////@Configuration
////@EnableWebSecurity
////@EnableGlobalMethodSecurity(prePostEnabled = true)
//public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
//
//    @Override
//    @Bean
//    public UserDetailsService userDetailsService() {
//        InMemoryUserDetailsManager memoryUserDetailsManager = new InMemoryUserDetailsManager();
//        memoryUserDetailsManager.createUser(User.withUsername("test").password("123").authorities("1").build());
//        return memoryUserDetailsManager;
//    }
//
//
////        @Override
////    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
////        auth.userDetailsService(userDetailsService).passwordEncoder(new PasswordEncoder() {
////            @Override
////            public String encode(CharSequence charSequence) {
////                return charSequence.toString();
////            }
////
////            @Override
////            public boolean matches(CharSequence charSequence, String s) {
////                return s.equals(charSequence.toString());
////            }
////        });
////    }
//    @Override
//    protected void configure(HttpSecurity http) throws Exception {
//        http.authorizeRequests()
//                // 如果有允许匿名的url，填在下面
////                .antMatchers().permitAll()
//                .antMatchers("/success").hasAuthority("1")
//                .antMatchers("/hello").hasAuthority("2")
//                .anyRequest().authenticated()
//
//                .and()
//                .sessionManagement()
//                .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
//                .and()
//                // 设置登陆页
//                .formLogin()
//                // 设置登陆成功页
//                .defaultSuccessUrl("/").permitAll()
//                // 自定义登陆用户名和密码参数，默认为username和password
//                .usernameParameter("username")
//                .passwordParameter("password")
//                .and()
//                .logout().permitAll();
//
////        http.addFilterAt(myAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);
//        // 关闭CSRF跨域
//        http.csrf().disable();
//    }
//
//    @Bean
//    MyUsernamePasswordAuthenticationFilter myAuthenticationFilter() throws Exception {
//        MyUsernamePasswordAuthenticationFilter filter = new MyUsernamePasswordAuthenticationFilter();
//        filter.setAuthenticationManager(authenticationManagerBean());
////        filter.setAuthenticationSuccessHandler(appLoginInSuccessHandler);
//        filter.setAuthenticationFailureHandler(new AuthenticationFailureHandler() {
//            @Override
//            public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) throws IOException, ServletException {
//                response.setContentType("application/json;charset=utf-8");
//                response.getWriter().write(JsonUtil.toJsonString(Response.fail("登录失败!")));
//            }
//        });
//        return filter;
//    }
//
//
//
//    @Bean
//    @Override
//    public AuthenticationManager authenticationManagerBean() throws Exception {
//        AuthenticationManager manager = super.authenticationManagerBean();
//        return manager;
//    }
//    @Bean
//    public PasswordEncoder passwordEncoder() {
//        return NoOpPasswordEncoder.getInstance();
//    }
//
//}
